package com.zzyl.controller;

import com.zzyl.base.ResponseResult;
import com.zzyl.dto.BedDto;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/bed")
@Api(tags = "床位管理相关接口")
public class BedController extends BaseController {

    @Autowired
    private BedService bedService;

    @GetMapping("/read/room/{roomId}")
    @ApiOperation(value = "根据房间id查询床位", notes = "传入房间id")
    public ResponseResult<List<BedVo>> readBedByRoomId(
            @ApiParam(value = "房间ID", required = true) @PathVariable("roomId") Long roomId) {
        List<BedVo> beds = bedService.getBedsByRoomId(roomId);
        return success(beds);
    }

    /**
     * 新增床位
     * @param bedDto
     * @return
     */
    @PostMapping("/create")
    @ApiOperation("新增床位接口")
    public ResponseResult createBed(
            @ApiParam(value = "床位dto实体类",required = true)@RequestBody BedDto bedDto){
        bedService.addBed(bedDto);
        return success();
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/read/{id}")
    @ApiOperation("根据id查询床位接口")
    @ApiImplicitParam(value = "床位id",name = "id",required = true)
    public ResponseResult<BedVo> readBed(@PathVariable Long id){
        try {
            BedVo bed = bedService.getById(id);
            return success(bed);
        } catch (Exception e) {
            throw new BaseException(BasicEnum.BED_INSERT_FAIL);
        }
    }

    /**
     * 修改床位
     * @param bedDto
     * @return
     */
    @PutMapping("/update")
    public ResponseResult update(@RequestBody BedDto bedDto){
        bedService.update(bedDto);
        return ResponseResult.success();
    }

    /**
     * 根据id 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public ResponseResult delete(@PathVariable Integer id){
        bedService.delete(id);
        return success();
    }

}
